package aad.storyboard.controller;

import java.util.HashMap;

import aad.storyboard.model.Story;
import aad.storyboard.model.menu.Menu;
import aad.storyboard.model.menu.MenuBuilder;
import aad.storyboard.model.menu.MenuProvider;
import aad.storyboard.view.MenuViewCreator;
import android.view.View;
import android.widget.RelativeLayout;

public class MenuViewProvider {
	
	private static MenuViewProvider instance = new MenuViewProvider();
	private MenuProvider provider;
	private View mainMenuView;
	private View pauseMenuView;
	private View settingsMenuView;
	private View finalView;
	private HashMap<Integer,View> sceneMenuViews;
	private View chapterMenuView;
	private Story story;
	private MenuBuilder builder;
	private MenuViewCreator viewCreator;
	
	
	public static void setup(Story story, MenuBuilder builder, MenuViewCreator creator){
		instance.story = story;
		instance.builder = builder;
		instance.viewCreator = creator ;
		instance.provider = MenuProvider.getInstance(story);
		instance.sceneMenuViews = new HashMap<Integer,View>();
	}

	public static MenuViewProvider getInstance() {
		return instance;
	}

	public View getMainMenuView() {
		System.out.println("HALLOMAIN");
		if(this.mainMenuView == null){
			provider.buildInitialMenu(builder);
			Menu menu = builder.getMenu();
			this.mainMenuView = viewCreator.createMenuView(menu);
		}
		return mainMenuView;
	}

	public View getPauseMenuView() {
		if(this.pauseMenuView == null){
			provider.buildPauseMenu(builder);
			Menu menu = builder.getMenu();
			this.pauseMenuView = viewCreator.createMenuView(menu);
		}
		return pauseMenuView;
	}

	public View getFinalView() {
		if(this.finalView == null){
			provider.buildFinalMenu(builder);
			Menu menu = builder.getMenu();
			this.finalView = viewCreator.createMenuView(menu);
		}
		return finalView;
	}

	public View getSceneMenuView(int i) {
		Integer key = new Integer(i);
		if(this.sceneMenuViews.get(key) == null){
			System.out.println("Creating scenes for chapter "+i);
			provider.buildSceneMenu(builder, i);
			sceneMenuViews.put(key, viewCreator.createMenuView(builder.getMenu()));
		}
		return sceneMenuViews.get(new Integer(i));
	}

	public View getChapterMenuView() {
		System.out.println("HALLOCHAPTER");
		if(this.chapterMenuView == null){
			provider.buildChapterSelectionMenu(builder);
			this.chapterMenuView = viewCreator.createMenuView(builder.getMenu());
		}
		return this.chapterMenuView;
	}

	public View getSettingsMenuView() {
		if(this.settingsMenuView == null){
			provider.buildSettingsMenu(builder);
			Menu menu = builder.getMenu();
			this.settingsMenuView = viewCreator.createMenuView(menu);
		}
		return settingsMenuView;
	}
	
	
}
